package LoginSystem;

import EncapsulatingObjects.User;
import Util.Captcha;
import Util.Inspectionrules;
import Util.UiUtil;
import Util.UserDatabaseUtil;

import javax.swing.*;
import javax.swing.border.MatteBorder;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class RetrievePassword {
    JFrame jf;
    JLabel account;
    JLabel tel;
    JLabel code;//验证码文本
    Captcha vfcd;//显示数字验证码
    JTextField tverificationCode;//验证码输入框

    JTextField taccount;
    JTextField ttel;

    JLabel login;
    JLabel register;
    JButton retrieve;
    UiUtil ui = new UiUtil();

    RetrievePassword() {
        init();
        ui.setFrameCenter(jf);
        ui.setFrameImage(jf);
        ui.setLogo(jf);
    }

    private void init() {
        jf = new JFrame("Retrieve");
        jf.setLayout(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        account = new JLabel("Account");
        tel = new JLabel("Phone");

        taccount = new JTextField(20);
        taccount.setBackground(new Color(255, 229, 138, 255));
        MatteBorder border = new MatteBorder(0, 0, 2, 0, new Color(17, 191, 191, 255));
        taccount.setBorder(border);
        ttel = new JTextField(20);
        ttel.setBackground(new Color(255, 229, 138, 255));
        ttel.setBorder(border);
        ttel.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if(e.getKeyChar() == '\n')
                    retrieveClicked();
            }
        });
        vfcd = new Captcha();
        vfcd.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        login = new JLabel("LOGIN");
        login.setForeground(Color.gray);
        register = new JLabel("REGISTER");
        register.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                registerClicked(e);
            }
        });
        retrieve = new JButton("Retrieve");
        register.setForeground(Color.gray);
        login.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        register.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        retrieve.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

        code = new JLabel("Captcha");
        tverificationCode = new JTextField(20);
        tverificationCode.setBackground(new Color(255, 229, 138, 255));
        tverificationCode.setBorder(border);

        ttel.setBorder(border);

        jf.setSize(500, 380);

        account.setBounds(65, 80, 70, 30);
        tel.setBounds(65, 130, 70, 30);

        taccount.setBounds(120, 80, 300, 30);
        ttel.setBounds(120, 130, 300, 30);

        code.setBounds(65, 180, 70, 30);
        tverificationCode.setBounds(120, 180, 150, 30);
        vfcd.setBounds(290, 175, 80, 30);

        retrieve.setBounds(150, 240, 200, 50);

        login.setBounds(40, 300, 80, 30);
        register.setBounds(380, 300, 80, 30);

        retrieve.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                retrieveClicked();
            }
        });
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                loginClicked();
            }
        });

        jf.add(account);
        jf.add(tel);
        jf.add(vfcd);
        jf.add(code);
        jf.add(taccount);
        jf.add(ttel);
        jf.add(tverificationCode);
        jf.add(login);
        jf.add(register);
        jf.add(retrieve);
        jf.setVisible(true);
    }

    private void registerClicked(MouseEvent e) {
        goResigter();
    }

    private void loginClicked() {
        goLogin();
    }

    private void retrieveClicked() {
        User user = new User();
        try {
            UserDatabaseUtil ud = new UserDatabaseUtil();
            user.setAccount(taccount.getText().trim());
            user.setPhone(ttel.getText().trim()) ;
            String vc = tverificationCode.getText().trim();

            if (user.getAccount().equals("") || user.getPhone().equals("")) {
                JOptionPane.showMessageDialog(jf, "Please enter all information");
                vfcd.nextCode();
                return;
            }

            if (!Inspectionrules.checkAccount(jf,user.getAccount()) || !Inspectionrules.checkTel(jf,user.getPhone())) {
                return;
            }

            if (!vc.equals(String.valueOf(vfcd.getCode()))) {
                JOptionPane.showMessageDialog(jf, "Please enter the right verification code");
                vfcd.nextCode();
                return;
            }

            Connection conn = ud.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from userdata");//默认刚开始在第0行

            while (rs.next()) {//next()是一个布尔类型函数，当有这一行存在时返回true
                if (user.getAccount().equals(rs.getString("account")) && user.getPhone().equals(rs.getString("tel"))) {//getInt(int 第几列/列名)
                    if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(jf, "Password recovered successfully!"
                            + "\nYour ID is : " + rs.getString("account")
                            + "\nYour phone number is : " + rs.getString("tel")
                            + "\nYour password is : " + rs.getString("password")
                            + "\nPlease remember your password!")) {
                        goLogin();
                    }
                    stmt.close();
                    return;
                }
            }
            stmt.close();
            JOptionPane.showMessageDialog(jf, "Failed to retrieve the password. Please check whether the information you entered is incorrect");
            vfcd.nextCode();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    private void goResigter() {
        jf.dispose();
        RegisterPage r = new RegisterPage();
    }

    private void goLogin() {
        jf.dispose();
        LoginPage l = new LoginPage();
    }
}
